/*
 * @copyright 2010 Gerald Jacobson
 * @license GNU General Public License
 * 
 * This file is part of Descent Assistant.
 *
 * Descent Assistant is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Descent Assistant is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Descent Assistant.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.amphiprion.descenttool.dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.amphiprion.descenttool.Home;
import org.amphiprion.descenttool.entity.Level;
import org.amphiprion.descenttool.util.ApplicationConstants;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.os.Environment;
import android.util.Log;

/**
 * @author Amphiprion
 * 
 */
public class LevelDao extends AbstractDao {
	private static LevelDao instance;

	public static LevelDao getInstance() {
		if (instance == null) {
			instance = new LevelDao();
		}
		return instance;
	}

	public List<Level> getLevels() {
		List<Level> result = new ArrayList<Level>();
		String lng = Locale.getDefault().getLanguage();
		try {
			File f = new File(Environment.getExternalStorageDirectory() + "/" + ApplicationConstants.DIRECTORY + "/definition.xml");

			InputStream is = null;
			if (f.exists()) {
				is = new FileInputStream(f);
			}
			if (is == null) {
				is = Home.class.getResourceAsStream("/definition.xml");
			}

			DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
			builderFactory.setNamespaceAware(true);

			DocumentBuilder builder = builderFactory.newDocumentBuilder();
			Document document = builder.parse(is);

			NodeList list = document.getElementsByTagName("def-level");
			for (int i = 0; i < list.getLength(); i++) {
				Element skillNode = (Element) list.item(i);
				String id = skillNode.getAttributes().getNamedItem("id").getNodeValue();

				// NAME
				String name = "";
				NodeList langs = skillNode.getElementsByTagName("lang");
				for (int iNames = 0; iNames < langs.getLength(); iNames++) {
					Element lang = (Element) langs.item(iNames);
					String local = getAttribute(lang, "id", "");
					if (local.equals(lng)) {
						name = langs.item(iNames).getFirstChild().getNodeValue();
						break;
					} else if ("en".equals(local)) {
						name = langs.item(iNames).getFirstChild().getNodeValue();
					}
				}

				Level level = new Level();
				level.id = id;
				level.name = name;
				result.add(level);
			}
			is.close();
			return result;
		} catch (Throwable e) {
			Log.e(ApplicationConstants.PACKAGE, "", e);
			return null;
		}
	}

}
